package com.cn.jdia.dc.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cn.jdia.dc.context.SpringContextUtils;
import com.cn.jdia.dc.model.Backupconfig;
import com.cn.jdia.dc.model.Dbbackupfile;
import com.cn.jdia.dc.service.IBackupconfigService;
import com.cn.jdia.dc.service.IDbbackupfileService;
import com.google.gson.Gson;

@Controller
public class BackupfileController {

	@Resource
	private IBackupconfigService backupconfigService;

	@Resource
	private IDbbackupfileService backupfileService;

	@RequestMapping("/backupfile")
	@ResponseBody
	public ModelAndView backupfile() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("backupfile");

		backupconfigService = (IBackupconfigService) SpringContextUtils.getContext().getBean("backupconfigService");
		List<Backupconfig> allBackupconfig = backupconfigService.getAllBackupconfig();

		List<String> projectList = new ArrayList<>();
		for (Backupconfig tmp : allBackupconfig) {
			projectList.add(tmp.getProject());
		}

		mav.addObject("list", projectList);
		return mav;
	}

	@RequestMapping(value = "/projectselect", produces = "application/text;charset=utf-8")
	@ResponseBody
	public String projectSelect(String project) {
		// System.out.println(project);

		backupfileService = (IDbbackupfileService) SpringContextUtils.getContext().getBean("dbbackupfileService");
		List<Dbbackupfile> allFile = backupfileService.getBackupFileByProject(project);

		List<Map<String, String>> list = new ArrayList<>();
		Map<String, String> map;
		String filename = null;

		for (int i = 0; i < allFile.size(); i++) {
			Dbbackupfile file = allFile.get(i);
			String[] split = file.getFilename().split("/");
			filename = split[split.length - 1];

			map = new HashMap<>();
			map.put("name", filename);
			map.put("size", file.getFilesize() + "");
			map.put("date", file.getCreatetime());

			list.add(map);
		}

		Gson gson = new Gson();
		String json = gson.toJson(list);

		// System.out.println(json);
		return json;
	}

	@RequestMapping(value = "/backupdownload")
	public ResponseEntity<byte[]> fileDownLoad(HttpServletRequest request) {

		String project = request.getParameter("project");
		String filename = request.getParameter("file");

		// System.out.println("download file");
		// System.out.println(project + " : " + filename);

		backupfileService = (IDbbackupfileService) SpringContextUtils.getContext().getBean("dbbackupfileService");
		Dbbackupfile downloadFile = backupfileService.getDownloadFilePath(project, filename);

		String pathString = downloadFile.getFilename();
		// String pathString = "D:\\Test\\config1";

		System.out.println(pathString);

		ResponseEntity<byte[]> re = null;

		try {
			File file = new File(pathString);

			if (!file.exists()) {
				System.out.println("download file doesn't exist");
				return null;
			}

			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

			byte[] by = FileUtils.readFileToByteArray(file);

			// This will cause filename be full path
			// headers.setContentDispositionFormData("attachment", pathString);
			headers.add("Content-Disposition", "attchement;filename=" + filename);
			headers.setContentLength(by.length);

			re = new ResponseEntity<byte[]>(by, headers, HttpStatus.CREATED);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return re;
	}

}
